#include"cla_stu.h"
#include<QDebug>
#include<QFile>
#include<QFileDialog>
#include<QMessageBox>
#include<QTextStream>
#include<QDebug>
#include<QTextEdit>
#include<QLineEdit>
#include<QTextCodec>
//构造函数
Stu::Stu(){

}

void Stu::myswap(QVector<StuNode>::iterator a,QVector<StuNode>::iterator b){
    QString A,B,C,D,E,F;
    A=a->num;B=a->name;C=a->sex;D=a->lou;E=a->qin;F=a->bed;
    a->num=b->num;a->name=b->name;a->sex=b->sex;a->lou=b->lou;a->qin=b->qin;a->bed=b->bed;
    b->num=A;b->name=B;b->sex=C;b->lou=D;b->qin=E;b->bed=F;
}

void Stu::entStu(QString fileName,StuNode s){
    QFile file(fileName);                                                      
    file.open(QIODevice::Text|QIODevice::ReadOnly);
    QString str=file.readLine();
    StuNode st;
    QString l;
    QStringList strlist;
   while(!file.atEnd()){
       l=file.readLine();
       strlist=l.split(",");
       st.num=strlist[0];
       st.name=strlist[1];
       st.sex=strlist[2];
       st.lou=strlist[3];
       st.qin=strlist[4];
       st.bed=strlist[5];
       vec.push_back(st);
   }                                              //先把文件内容存入向量
    file.close();
    //写入文件
    file.open(QIODevice::Text|QIODevice::Append);
    QTextStream in(&file);
    in.setCodec("UTF-8");
    if(str.isEmpty()){                                //写文件前判断文件是否为空,为空直接写入,形式不变
    in<<s.num<<","<<s.name<<","<<s.sex<<","<<s.lou<<","<<s.qin<<","<<s.bed;
    }else{
        int sign=0;
        for(auto it=vec.begin();it<vec.end();it++){   //查重
                if(it->num==s.num){
                    sign=1;
                    break;
                }
       }
     if(!sign){                                    //输入内容与文件中无重复保存
           in<<endl<<s.num<<","<<s.name<<","<<s.sex<<","<<s.lou<<","<<s.qin<<","<<s.bed;
     }else{                                       //有重复,提示退出
      QMessageBox::information(0,QString("提示"),QString("该学生已存在"),QMessageBox::Ok);
     }
  }
    file.close();
}//录入信息实现
QStringList Stu::readAll(QString fileName,int &r_count){
    if(!fileName.isEmpty()){
        QFile file;
        file.setFileName(fileName);
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
            QMessageBox msg;
            msg.setText("打开文件失败");
            msg.exec();
        }else {
           if(!file.size()){
               QMessageBox::warning(0,QString("警告"),QString("信息不存在!"),QMessageBox::Ok);
           }else{

               QStringList textlist;
               QTextStream in(&file);
               in.setCodec("UTF-8");
               while(!in.atEnd()){
                   QString line=in.readLine();
                   textlist.append(line);
                   r_count++;
               }
               file.close();                             //将文件内容读入,保存在textlist中,并记录行数
               return textlist;
            }
        }
    }else{
           QMessageBox::warning(0,QString("警告"),QString("文件名未指定"),QMessageBox::Ok);
           }

}//浏览信息实现
QStringList Stu::readOne(QString fileName,QString filePath,int &r_count,QString str1,QString str2){
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        QFile f(filePath);
        f.resize(0);
        f.open(QIODevice::Append|QIODevice::Text);
        QTextStream in(&f);
        in.setCodec("UTF-8");
        StuNode s;
        QString l;
        QStringList strlist;
       while(!file.atEnd()){
           l=file.readLine();
           strlist=l.split(",");
           s.num=strlist[0];
           s.name=strlist[1];
           s.sex=strlist[2];
           s.lou=strlist[3];
           s.qin=strlist[4];
           s.bed=strlist[5];
           vec.push_back(s);
       }                                        //先把文件内容存入向量
       file.close();
       for(auto it=vec.begin();it<vec.end();it++){
           if(it->lou==str1&&it->qin==str2){
           in<<it->num<<","<<it->name<<","<<it->sex<<","<<it->lou<<","<<it->qin<<","<<it->bed;
          }
       }                                        //筛选符合的数据,存入中间文件
       f.close();
       f.open(QIODevice::ReadOnly|QIODevice::Text);
       QString str=f.readLine();
       if(!str.isEmpty()){
       QStringList textlist=readAll(filePath,r_count);//找到符合数据,存入list
       return textlist;
       }else{
           QStringList t;
           return t;                                   //未找到符合数据,返回空的list
       }
}
}

bool Stu::deleOne(QString fileName,QString str){
    vec.clear();
    QFile file(fileName);
    file.open(QIODevice::Text|QIODevice::ReadOnly);
    if(!file.exists()){
         QMessageBox::information(0,QString("error"),QString("该文件为空"),QMessageBox::Ok);
    }else{
    StuNode s;
    QString l;
    QStringList strlist;
   while(!file.atEnd()){
       l=file.readLine();
       strlist=l.split(",");
       s.num=strlist[0];
       s.name=strlist[1];
       s.sex=strlist[2];
       s.lou=strlist[3];
       s.qin=strlist[4];
       s.bed=strlist[5];
       vec.push_back(s);
   }//先把文件内容存入向量

//删除操作
    file.close();
    QVector<StuNode>::iterator it;
    int sig=0;
    for(it=vec.begin();it!=vec.end();it++){
        if(it->num==str){
            myswap(it,vec.begin());
            vec.pop_front();
            sig=1;
        }
    }
    if(sig){
        //保存操作
           //保存
              file.open(QIODevice::WriteOnly|QIODevice::Text);
              QTextStream in(&file);
              in.setCodec("UTF-8");
              QVector<StuNode>::iterator t;
              for(t=vec.begin();t!=vec.end();t++){
                  in<<t->num<<","<<t->name<<","<<t->sex<<","<<t->lou<<","<<t->qin<<","<<t->bed;
              }
              file.close();
    }
    return sig;
   }
}//删除信息实现

bool Stu::inquStu(QString fileName,QString &ser){
    vec.clear();
    QFile file(fileName);
    file.open(QIODevice::Text|QIODevice::ReadOnly);
    if(!file.exists()){
         QMessageBox::information(0,QString("error"),QString("该文件为空"),QMessageBox::Yes);
    }else{
     StuNode s;
     QString l;
     QStringList strlist;
    while(!file.atEnd()){
        l=file.readLine();
        strlist=l.split(",");
        s.num=strlist[0];
        s.name=strlist[1];
        s.sex=strlist[2];
        s.lou=strlist[3];
        s.qin=strlist[4];
        s.bed=strlist[5];
        vec.push_back(s);
    }
    file.close();
    bool sig=0;
    QString str;
    QVector<StuNode>::iterator it;
    for(it=vec.begin();it!=vec.end();it++){
        if(it->num==ser){
            str+=it->num;
            str+=" ";
            str+=it->name;
            str+=" ";
            str+=it->sex;
            str+=" ";
            str+=it->lou;
            str+=" ";
            str+=it->qin;
            str+=" ";
            str+=it->bed;     //
            sig=1;
        }
    }
    ser=str;
    return sig;
   }
}//查询信息实现
bool Stu::modfStu(QString fileName,QString num){
  bool sig=deleOne(fileName,num);
  return sig;
}

